const app = getApp();

Page({
    data: {
        defaultAvatar: "http://img.zhxcxmall.com/static/default-avatar3.png",
        account:false,
        supplier:2
    },
    loadStore() {
        app.http.post('supplier/has-store').then(res => {
            console.log(res.data)
            if(res.data.supplier != null){
                this.setData({
                    supplier: 1
                })
            }

            if(res.data==null){
                this.setData({
                    account:false
                })
            }else{
                this.setData({
                    account: true,
                    store_id: res.data.store_id,
                    uid:res.data.user_id||'',
                    username: res.data.user ? res.data.user.username : '',
                    pwd: res.data.user ? res.data.user.password_hash : '',
                })
            }
        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },
    onShow: function () {
        this.loadStore()
        app.toPromise(wx.getStorage, { key: "user" }).then(res => {
            return { userInfo: res.data }
        }).catch(err => {
            return app.toPromise(wx.getUserInfo)
        }).then(res => {
            if (res.userInfo) {
                this.setData({
                    user: res.userInfo
                })
                app.http.post("member/save-info", {
                    data: res.userInfo
                })
                wx.setStorage({
                    key: 'user',
                    data: res.userInfo,
                })
            }
        }).catch(err => {

        })
    },
    getUser(evt) {
        const user = evt.detail.userInfo
        if (user) {
            wx.setStorage({
                key: 'user',
                data: user,
            })
            this.setData({
                user
            })
            app.http.post("member/save-info", {
                data: user
            })
        }
    },

})